package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import model.User;
import service.UserServiceImpl;

public class UpdateDialog extends JDialog implements ActionListener {
	JButton jbtupdate,jbtcancell;
	JLabel jlbuserid,jlbpassword,jlbusername;
	JTextField jtfuserid,jtfpassword,jtfusername;
	
	
     UpdateDialog(){
    	 	setTitle("用户服务");
    	   	setSize(220,210);
    	   	setLocationRelativeTo(null);
    	   	setResizable(false);
    	   	setLayout(null);
    	   	jlbuserid=new JLabel("用户账号");
    	   	jlbpassword=new JLabel("用户密码");
    	   	jlbusername=new JLabel("用户名称");
    	   	
    	   	jbtupdate=new JButton("更新信息");
    	   	jbtcancell=new JButton("退出");
    	   	
    	   	jtfuserid=new JTextField(20);
    	   	jtfpassword=new JTextField(20);
    	   	jtfusername=new JTextField(20);
    	   	
    	   	jlbuserid.setBounds(20, 20, 60, 20);  add(jlbuserid); 
    	   	jlbpassword.setBounds(20, 70, 60, 20); add(jlbpassword);
    	   	jlbusername.setBounds(20, 120, 60, 20);  add(jlbusername);
    	   	
    	   	jtfuserid.setBounds(80, 20, 110, 20);  add(jtfuserid);
    	   	jtfpassword.setBounds(80, 70, 110, 20);  add(jtfpassword);
    	   	jtfusername.setBounds(80, 120, 110, 20);add(jtfusername);
    	   	
    	   	jtfuserid.setEditable(false);
    	   	jtfpassword.setEditable(true);
    	   	jtfusername.setEditable(true);
    	   	
    	   	jbtupdate.setBounds(20, 150, 90, 20); add(jbtupdate); jbtupdate.addActionListener(this);
    	   	jbtcancell.setBounds(120, 150, 90, 20);add(jbtcancell);jbtcancell.addActionListener(this);
    	   	
    	   	setVisible(true);
     }
     
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==jbtcancell){
			dispose();
		}
		
		if(e.getSource()==jbtupdate){			
    	   UserServiceImpl uri=new UserServiceImpl();
    	   User user=new User();
    	   user.setUserid(Integer.parseInt(jtfuserid.getText().trim()));
    	   user.setPassword(jtfpassword.getText().trim());
    	   	user.setUsername(jtfusername.getText().trim());
    	   	
    	   	
    	   	
    	   	try{
    	   	if(uri.updateUser(user)){
    	   		JOptionPane.showMessageDialog(null, "更新成功", "提示",
						JOptionPane.NO_OPTION);
    	   	}
    	   	else{
    	   		JOptionPane.showMessageDialog(null, "更新失败", "出错警告",
						JOptionPane.NO_OPTION);
    	   	}
    	   	dispose();
    	   	new MainFrame(user);
    	   	}
    	   	catch(Exception e1){
    	   		e1.getStackTrace();
    	   	}
		}
		
		
	}
     
     
}
